// component/component/uploadImage/uploadImage.js
const app = getApp()
const util = require('../../../utils/util.js')
const lbbService = require('../../../pages/service/service.js')
var Base64 = require('../../../utils/base64.js')
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        maxImgLength: {
            type: Number,
            value: 9,
        },
        imgArray: {
            type: Array,
        },
        ifShowDel: {
            type: Boolean,
            value: true
        }
    },

    /**
     * 组件的初始数据
     */
    data: {
        imgArray: [],
    },

    /**
     * 组件的方法列表
     */
    methods: {

        addImg: function (e) {
            let that = this;
            wx.chooseMedia({
                count: that.data.maxImgLength - that.data.imgArray.length,
                sizeType: ['compressed'],
                sourceType: ['album', 'camera'],
                success: function (res) {
                    console.log('img=>>>', res)
                    for (let i = 0; i < res.tempFiles.length; i++) {
                        if (res.tempFiles[i].size > 15000000) {
                            wx.showToast({
                                title: '图片大小超过15M',
                                icon: 'error',
                                duration: 2000
                            })
                            return
                        }
                        that.data.imgArray.push(res.tempFiles[i].tempFilePath)
                    }
                    console.log(that.data.imgArray)
                      that.setData({
                        imgArray: that.data.imgArray
                    })
                    that.triggerEvent('setimgarr', that.data.imgArray);
                }
            })
        },
        /**
         * 删除图片
         */
        deleteImg: function (e) {
            let that = this
            let deleteIndex = e.currentTarget.dataset.index
            that.data.imgArray.splice(deleteIndex, 1)
            that.setData({
                imgArray: that.data.imgArray
            })
            that.triggerEvent('setimgarr', that.data.imgArray);
        },

        /**
         * 图片预览
         */
        preViewImgs: function (e) {
            let current = e.currentTarget.dataset.current;
            if (app.globalData.arrayImage) {
                var urls = app.globalData.arrayImage;
            } else {
                var urls = e.currentTarget.dataset.urls;
            }
            util.previewImgs(current, urls);
        },
    }
})